﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ComicsEditor
{
    public partial class FontDialog : Form
    {
        private ComicInfo comicInfo;
        private List<FontEntry> fonts = new List<FontEntry>();

        private class FontEntry
        {
            private string fontName;
            private string embed;
            private string embedCurrent;

            public FontEntry(string fontName, string embed)
            {
                this.fontName = fontName;
                this.embed = embed;
                this.embedCurrent = embed;
            }

            public string FontName
            {
                get
                {
                    return fontName;
                }
            }

            public string Embed
            {
                get
                {
                    return embed;
                }
                set
                {
                    embed = value;
                }
            }

            internal string EmbedCurrent
            {
                get
                {
                    return embedCurrent;
                }
            }
        }

        public FontDialog()
        {
            InitializeComponent();
        }

        public ComicInfo ComicInfo
        {
            set
            {
                comicInfo = value;

                for (int i = 0; i < comicInfo.Count; i++)
                    foreach (Text text in comicInfo[i].Texts)
                        for (int j = 0; j < text.Formatted.Count; j++)
                            foreach (TextSpan span in text.Formatted[j])
                            {
                                string embed = span.Embedded ? "<embed curves>" : "";
                                if (embed == "" && (comicInfo.Fonts.ContainsKey(span.FontName) || comicInfo.Fonts.ContainsKey(span.FontFamily)))
                                    embed = "<embed font file>";
                                int k;
                                for (k = 0; k < fonts.Count && (fonts[k].FontName != span.FontFamily || fonts[k].Embed != embed); k++);
                                if (k >= fonts.Count)
                                    fonts.Add(new FontEntry(span.FontFamily, embed));
                            }
                dataGridView1.DataSource = fonts;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            SortedList<string, FontInfo> embeddedFonts = new SortedList<string, FontInfo>();
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");

            for (int i = 0; i < comicInfo.Count; i++)
                foreach (Text text in comicInfo[i].Texts)
                    for (int j = 0; j < text.Formatted.Count; j++)
                        foreach (TextSpan span in text.Formatted[j])
                        {
                            string embed = span.Embedded ? "<embed curves>" : "";
                            int k;
                            for (k = 0; k < fonts.Count && (fonts[k].FontName != span.FontFamily || fonts[k].EmbedCurrent != embed); k++);
                            if (k < fonts.Count)
                            {
                                if (fonts[k].EmbedCurrent != fonts[k].Embed)
                                    span.Embedded = fonts[k].Embed == "<embed curves>";
                                if (fonts[k].Embed == "<embed font file>")
                                {
                                    FontInfo fontInfo = null;
                                    if (comicInfo.Fonts.ContainsKey(span.FontName))
                                        fontInfo = comicInfo.Fonts[span.FontName];
                                    else if (key.GetValue(span.FontName + " (TrueType)") != null)
                                        fontInfo = new FontInfo(span.FontName);
                                    else if (comicInfo.Fonts.ContainsKey(span.FontFamily))
                                        fontInfo = comicInfo.Fonts[span.FontFamily];
                                    else if (key.GetValue(span.FontFamily + " (TrueType)") != null)
                                        fontInfo = new FontInfo(span.FontFamily);
                                    if (fontInfo != null && !embeddedFonts.ContainsKey(fontInfo.Name))
                                        embeddedFonts.Add(fontInfo.Name, fontInfo);
                                }
                            }
                        }
            
            comicInfo.Fonts = embeddedFonts;
            key.Close();
        }
    }
}
